﻿namespace HuaWei.CommComp
{
    using DevExpress.XtraBars;
    using HuaWei;
    using HuaWei.CommClass;
    using HuaWei.Common;
    using HuaWei.DAI;
    using HuaWei.Windows.Forms;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public class FrmLogin : Form
    {
        private BarEditItem barEditItem1;
        private BarEditItem barEditItem2;
        private Button btnCancel;
        private Button btnOK;
        private ComboBox cbYH;
        private DataSet dataSet = new DataSet();
        private BarEditItem edtBD;
        private TextBox edtPwd;
        private TextBox edtUser;
        private IContainer icontainer = null;
        private Label label3;
        private Label label4;
        private Label label5;
        private Panel panel1;
        private CheckBox chkSave;
        private PictureBox pictureBox1;

        public FrmLogin()
        {
            this.InitializeComponent();
            this.Text = "项目管理软件";
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.edtUser.Text.Trim()) && !string.IsNullOrEmpty(this.edtPwd.Text.Trim()))
            {
                this.CopyDataFileAndSetCeIDA();
                if (this.method1(this.edtUser.Text, this.edtPwd.Text))
                {
                    if (this.edtUser.Text.ToLower().Equals("admin"))
                    {
                        GParams._JGLX = JGLX.GLY;
                        GParams.CXJZ = true;
                    }
                    if (GParams._JGLX.Equals(JGLX.GLY))
                    {
                        if (this.method4("GetXMJBXX"))
                        {
                            base.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (this.IsCeZZJGXXEmpty())
                    {
                        if (this.method4("GetAllData"))
                        {
                            base.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (this.method4("GetUpdateData"))
                    {
                        base.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入用户名和密码！");
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.icontainer != null))
            {
                this.icontainer.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            GParams.APP_PATH = AppDomain.CurrentDomain.BaseDirectory;
            GParams.SysFilePath = GParams.APP_PATH + "system.ini";
            string isSave = IniFile.ReadIniFile(GParams.SysFilePath, "SYS", "IsSave", false);
            if (isSave == "1")
            {
                this.edtUser.Text = IniFile.ReadIniFile(GParams.SysFilePath, "SYS", "UserName", false);
                this.edtPwd.Text = IniFile.ReadIniFile(GParams.SysFilePath, "SYS", "Password", true);
                this.chkSave.Checked = true;
            }
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmLogin));
            this.panel1 = new System.Windows.Forms.Panel();
            this.label5 = new System.Windows.Forms.Label();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnOK = new System.Windows.Forms.Button();
            this.edtPwd = new System.Windows.Forms.TextBox();
            this.edtUser = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.edtBD = new DevExpress.XtraBars.BarEditItem();
            this.barEditItem1 = new DevExpress.XtraBars.BarEditItem();
            this.cbYH = new System.Windows.Forms.ComboBox();
            this.barEditItem2 = new DevExpress.XtraBars.BarEditItem();
            this.chkSave = new System.Windows.Forms.CheckBox();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.chkSave);
            this.panel1.Controls.Add(this.label5);
            this.panel1.Controls.Add(this.btnCancel);
            this.panel1.Controls.Add(this.btnOK);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel1.Location = new System.Drawing.Point(0, 156);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(356, 48);
            this.panel1.TabIndex = 22;
            // 
            // label5
            // 
            this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label5.Dock = System.Windows.Forms.DockStyle.Top;
            this.label5.Location = new System.Drawing.Point(0, 0);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(356, 2);
            this.label5.TabIndex = 11;
            // 
            // btnCancel
            // 
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(259, 13);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 1;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = true;
            // 
            // btnOK
            // 
            this.btnOK.Location = new System.Drawing.Point(178, 13);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(75, 23);
            this.btnOK.TabIndex = 0;
            this.btnOK.Text = "确定";
            this.btnOK.UseVisualStyleBackColor = true;
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            // 
            // edtPwd
            // 
            this.edtPwd.Location = new System.Drawing.Point(112, 118);
            this.edtPwd.Name = "edtPwd";
            this.edtPwd.PasswordChar = '*';
            this.edtPwd.Size = new System.Drawing.Size(198, 21);
            this.edtPwd.TabIndex = 1;
            this.edtPwd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.edtPwd_KeyDown);
            // 
            // edtUser
            // 
            this.edtUser.Location = new System.Drawing.Point(112, 91);
            this.edtUser.Name = "edtUser";
            this.edtUser.Size = new System.Drawing.Size(198, 21);
            this.edtUser.TabIndex = 0;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(43, 124);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(29, 12);
            this.label4.TabIndex = 29;
            this.label4.Text = "密码";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(43, 97);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 12);
            this.label3.TabIndex = 28;
            this.label3.Text = "用户名";
            // 
            // pictureBox1
            // 
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(356, 72);
            this.pictureBox1.TabIndex = 23;
            this.pictureBox1.TabStop = false;
            // 
            // edtBD
            // 
            this.edtBD.Caption = "ss";
            this.edtBD.Edit = null;
            this.edtBD.Id = 12;
            this.edtBD.Name = "edtBD";
            // 
            // barEditItem1
            // 
            this.barEditItem1.Caption = "ss";
            this.barEditItem1.Edit = null;
            this.barEditItem1.Id = 12;
            this.barEditItem1.Name = "barEditItem1";
            // 
            // cbYH
            // 
            this.cbYH.FormattingEnabled = true;
            this.cbYH.Items.AddRange(new object[] {
            "三标段",
            "四标段",
            "三驻地办",
            "一高监办",
            "合同部",
            "管理处二",
            "管理员"});
            this.cbYH.Location = new System.Drawing.Point(136, 91);
            this.cbYH.Name = "cbYH";
            this.cbYH.Size = new System.Drawing.Size(198, 20);
            this.cbYH.TabIndex = 30;
            this.cbYH.Visible = false;
            // 
            // barEditItem2
            // 
            this.barEditItem2.Caption = "ss";
            this.barEditItem2.Edit = null;
            this.barEditItem2.Id = 12;
            this.barEditItem2.Name = "barEditItem2";
            // 
            // chkSave
            // 
            this.chkSave.AutoSize = true;
            this.chkSave.Location = new System.Drawing.Point(45, 13);
            this.chkSave.Name = "chkSave";
            this.chkSave.Size = new System.Drawing.Size(72, 16);
            this.chkSave.TabIndex = 12;
            this.chkSave.Text = "记住密码";
            this.chkSave.UseVisualStyleBackColor = true;
            this.chkSave.CheckedChanged += new System.EventHandler(this.chkSave_CheckedChanged);
            // 
            // FrmLogin
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(356, 204);
            this.Controls.Add(this.cbYH);
            this.Controls.Add(this.edtPwd);
            this.Controls.Add(this.edtUser);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.panel1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "FrmLogin";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Load += new System.EventHandler(this.FrmLogin_Load);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void method()
        {
            base.Close();
            Environment.Exit(Environment.ExitCode);
        }

        private bool method1(string _str, string _str1)
        {
            int num = Class38.smethod3(_str, _str1);
            if (num < 0)
            {
                this.edtUser.Focus();
                return false;
            }
            if (num.Equals(0))
            {
                string pToDecrypt = IniFile.ReadIniFile(GParams.SysFilePath, "SYS", "MID", false);
                if (!pToDecrypt.Equals(""))
                {
                    GParams.LoginId = EncryptDecrypt.smethod2(pToDecrypt, "DdTtMmJl");
                    GParams._JGLX = (JGLX)CHelp.ConvertInt(EncryptDecrypt.smethod2(IniFile.ReadIniFile(GParams.SysFilePath, "SYS", "MLX", false), "DdTtMmJl"), 0);
                    IDataAccess idataAccess = DBHelp.IniIDA();
                    string str3 = string.Format("select * from YHXX where bdid='{0}'", GParams.LoginId);
                    Class39.smethod4(idataAccess, this.dataSet, str3, "YHXX", true);
                    DataRow[] rowArray = this.dataSet.Tables["YHXX"].Select(string.Format("YHMC='{0}' and YHMM='{1}'", _str, _str1));
                    idataAccess.CloseDatabase();
                    int length = rowArray.Length;
                    if (length.Equals(0))
                    {
                        MsgBox.ShowMsg("用户名或密码不正确，请重新输入！");
                        this.edtUser.Focus();
                        return false;
                    }
                    return true;
                }
                MsgBox.ShowMsg("第一次使用请与管理员联系获取正确的登录信息！");
                return false;
            }
            return true;
        }

        private bool CopyDataFileAndSetCeIDA()
        {
            if (!File.Exists(GParams.APP_PATH + "JLZFCore.dtm"))
            {
                File.Copy(GParams.APP_PATH + "Core.dtm", GParams.APP_PATH + "JLZFCore.dtm");
                GParams.IsEmptyDB = true;
            }
            DBHelp.IDA = DBHelp.IniIDA("MSSQLCe", "JLZFCore.dtm");

            //判断期次表是否存在支付编号字段，没有则添加
            string sql = "select * from XMQC WHERE 1=2";
            DataSet ds = DBHelp.IDA.ExecuteDataSet(sql, CommandType.Text);
            if (!ds.Tables[0].Columns.Contains("ZFBH"))
            {
                sql = "alter table XMQC add ZFBH nvarchar(50);";
                DBHelp.IDA.ExecuteNonQuery(sql, CommandType.Text);
            }
            if (!ds.Tables[0].Columns.Contains("ZFBHForThree"))
            {
                sql = "alter table XMQC add ZFBHForThree nvarchar(50);";
                DBHelp.IDA.ExecuteNonQuery(sql, CommandType.Text);
            }
            if (!ds.Tables[0].Columns.Contains("ZFBHForFour"))
            {
                sql = "alter table XMQC add ZFBHForFour nvarchar(50);";
                DBHelp.IDA.ExecuteNonQuery(sql, CommandType.Text);
            }
            sql = "alter table REP_ZQZFZS alter column pzbh nvarchar(4000);";
            DBHelp.IDA.ExecuteNonQuery(sql, CommandType.Text);
            sql = "alter table REP_ZQZFZS alter column zh nvarchar(4000);";
            DBHelp.IDA.ExecuteNonQuery(sql, CommandType.Text);
            sql = "alter table YHXX alter column JGMC nvarchar(50);";
            DBHelp.IDA.ExecuteNonQuery(sql, CommandType.Text);

            sql = "select * from REP_GCBG WHERE 1=2";
            DataSet dsGCBG = DBHelp.IDA.ExecuteDataSet(sql, CommandType.Text);
            if (!dsGCBG.Tables[0].Columns.Contains("BGLH"))
            {
                sql = "alter table REP_GCBG add BGLH nvarchar(50);";
                DBHelp.IDA.ExecuteNonQuery(sql, CommandType.Text);
            }

            return true;
        }

        private bool IsCeZZJGXXEmpty()
        {
            IDataAccess ida = DBHelp.IniIDA();
            string sql = string.Format("select * from ZZJGXX", new object[0]);
            return (DBHelp.GetDt(ida, sql, "ZZJGXX").Rows.Count <= 0);
        }

        private bool method4(string _str)
        {
            Interface1 interface2 = Class37.smethod();
            if (_str.Equals("GetAllData"))
            {
                return interface2.imethod_0(_str, GParams.LoginId);
            }
            else
            {
                if (interface2.imethod_0(_str, GParams.LoginId))
                {
                    return Class38.smethod2(GParams.LoginId);
                }
                else
                {
                    return false;
                }
            }
        }

        private void edtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOK_Click(null, null);
            }
        }

        private void chkSave_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSave.Checked)
            {
                edtUser.Enabled = false;
                edtPwd.Enabled = false;
                IniFile.WriteIniFile(GParams.SysFilePath, "SYS", "IsSave", "1", false);
                IniFile.WriteIniFile(GParams.SysFilePath, "SYS", "UserName", edtUser.Text.Trim(), false);
                IniFile.WriteIniFile(GParams.SysFilePath, "SYS", "Password", edtPwd.Text.Trim());
            }
            else
            {
                edtUser.Enabled = true;
                edtPwd.Enabled = true;
                IniFile.WriteIniFile(GParams.SysFilePath, "SYS", "IsSave", "0", false);
                IniFile.WriteIniFile(GParams.SysFilePath, "SYS", "UserName", "", false);
                IniFile.WriteIniFile(GParams.SysFilePath, "SYS", "Password", "");
            }
        }
    }
}

